package src;

import java.io.BufferedWriter;
import java.util.Map;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;

public class dataManager {
	
	private Map<Integer, String> puffer;
	private SynchronizedCounter logSeqNr;
	private ArrayList<Integer> openTransactions;
	
	public dataManager (){
		 puffer = new ConcurrentHashMap<Integer, String>();
		 logSeqNr = new SynchronizedCounter();
		 openTransactions = new ArrayList<Integer>();
	}
	
	public void write(int taid, int pageid, String data){
		Integer LogNr = writeLogFile(taid, pageid, data);
		puffer.put(pageid,String.valueOf(taid+";"+LogNr+";"+data));
		//System.out.println("Schreibe Seite "+pageid+" aus Transaktion "+taid+ " in den Puffer");
		//System.out.println(puffer);
		
//		//Liste der offenen transaktionen
//		Boolean bfound = false;
//		for (Integer item : openTransactions){
//			if (item == taid){
//				bfound = true;
//			}	
//		}
//		if (bfound == false){
//			openTransactions.add(taid);
//		}
	}
	
	public void pruefePuffer(int taid){
		if (puffer.size() > 5){
		//	System.out.println(puffer);
			Iterator<Integer> keySetItr = puffer.keySet().iterator();  
			  while( keySetItr.hasNext()) {
			   Integer key = keySetItr.next();
			   String gesuchteTaid = puffer.get(key).substring(0,puffer.get(key).indexOf(";"));
				if (Integer.parseInt(gesuchteTaid)==taid){
					writeToFile(taid, key, puffer.get(key));
					writeCommitLog(taid, key, puffer.get(key));
					try{
						keySetItr.remove();	
						//System.out.println("--> L�sche Seite "+key+" aus Transaktion "+taid);
					}
					catch (Exception e) {
						// TODO: handle exception
					}
					
					}
				}

			  }
//			for(Map.Entry<Integer,String> entry : puffer.entrySet()){
//				if (entry != null){
//				String gesuchteTaid = entry.getValue().substring(0,entry.getValue().indexOf(";"));
//				if (Integer.parseInt(gesuchteTaid)==taid){
//					writeToFile(taid, entry.getKey(), entry.getValue());
//					puffer.remove(entry.getKey());
//					}
//				}
//			}
	}
	
	public void writeToFile(int taid, int pageid, String data){
			try
			{
				//s
				File file = new File("./Data/Seite"+String.valueOf(pageid)+".txt");
				//System.out.println(file.getAbsolutePath());
				FileWriter fw = new FileWriter(file);
				
				fw.write(taid+";"+pageid+";"+ data);
				//System.out.println("Schreibe Seite "+pageid+" aus Transaktion "+taid+" persistent");
				fw.flush();
				fw.close();
			}
			catch( IOException e )
			{
				e.printStackTrace();
			}
		}
	
		public void readLogFile(){
			try {
				FileReader fr = new FileReader("./Data/Logfile.txt");
				BufferedReader br = new BufferedReader(fr);
				String s;
				while(true){
					String line =br.readLine();
					if(line != null){
						if(line.indexOf("Commit")>-1){
							System.out.println(line);	
						}		
					}
					else{
						break;
					}
				}
				fr.close();
			}
			catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		public int writeLogFile(int taid, int pageid, String data){
			int TempLogSeqNr;
			TempLogSeqNr =  logSeqNr.incrementAndGet();
			try
			{			
				FileWriter fw = new FileWriter("./Data/Logfile.txt",true);
				BufferedWriter out = new BufferedWriter(fw);
				out.write(TempLogSeqNr+";"+taid+";"+pageid+";"+ data);
				out.newLine();
				out.close();
				fw.close();
				
			}
			catch( IOException e )
			{
				e.printStackTrace();
			}
			return TempLogSeqNr;
		}
		
		public int writeCommitLog(int taid, int pageid, String data){
			int TempLogSeqNr;
			TempLogSeqNr =  logSeqNr.incrementAndGet();
			try
			{			
				FileWriter fw = new FileWriter("./Data/Logfile.txt",true);
				BufferedWriter out = new BufferedWriter(fw);
				out.write("Commit "+TempLogSeqNr+";"+taid+";"+pageid+";"+ data);
				out.newLine();
				out.close();
				fw.close();
				
			}
			catch( IOException e )
			{
				e.printStackTrace();
			}
			return TempLogSeqNr;
		}
}
